package com.hsyt.pfc.support.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * DES加密工具类
 *
* @Author: wangze@hsyuntai.com
 * 2015年4月9日 下午8:29:57
 */
public class HsMedDES {

    private static final String IV = "F5D9CCA7";

    private static SecretKey hsMedDesKey = new SecretKeySpec(("zaq1@WSX").getBytes(), "DES");

    /*
     * 创建密钥
     */
    public static SecretKey createKey() throws NoSuchAlgorithmException {

        KeyGenerator kg = KeyGenerator.getInstance("DES/CBC/PKCS5Padding");
        SecureRandom secureRandom = new SecureRandom();
        kg.init(secureRandom);
        SecretKey key = kg.generateKey();
        return key;
    }

    /**
     * 加密, 返回Base64编码的字符串
     *
     * @param srcStr
     * @return
     */
    public static String encrypt(String srcStr) {
        String rtn = null;
        try {
            IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
            byte[] srcByte = srcStr.getBytes("UTF-8");
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, hsMedDesKey, ivParameterSpec);
            byte[] enc = cipher.doFinal(srcByte);
            rtn = Base64.encodeBase64String(enc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtn;
    }

    /**
     * 加密, 返回Base64编码的字符串
     *
     * @param srcStr
     * @return
     */
    public static String encrypt(String srcStr, String keyStr) {
        String rtn = null;
        try {
            IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
            SecretKey key = new SecretKeySpec(keyStr.getBytes(), "DES");
            byte[] srcByte = srcStr.getBytes("UTF-8");
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
            byte[] enc = cipher.doFinal(srcByte);
            rtn = Base64.encodeBase64String(enc);
        } catch (Exception e) {
            System.out.println("加密出错!");
            e.printStackTrace();
        }
        return rtn;
    }

    /**
     * 对Base64编码的二进制字符串进行解密
     *
     * @param src
     * @return : 解密后的字符串
     */
    public static String decrypt(String src) {
        String rtn = null;
        try {
            IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
            byte[] b = Base64.decodeBase64(src);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, hsMedDesKey, ivParameterSpec);
            byte[] dec = cipher.doFinal(b);
            rtn = new String(dec);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtn;
    }

    public static String decrypt(String src, String keyStr) {
        String rtn = null;
        try {
            IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
            SecretKey key = new SecretKeySpec(keyStr.getBytes(), "DES");
            byte[] b = Base64.decodeBase64(src);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
            byte[] dec = cipher.doFinal(b);
            rtn = new String(dec);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtn;
    }

    public static String decryptNOIV(String src, String keyStr) {
        String rtn = null;
        try {
            SecretKey key = new SecretKeySpec(keyStr.getBytes(), "DES");
            byte[] b = Base64.decodeBase64(src);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] dec = cipher.doFinal(b);
            rtn = new String(dec);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtn;
    }

    public static String decryptWithThrowException(String src, String keyStr) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        String rtn = null;
        IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
        SecretKey key = new SecretKeySpec(keyStr.getBytes(), "DES");
        byte[] b = Base64.decodeBase64(src);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
        byte[] dec = cipher.doFinal(b);
        rtn = new String(dec);
        return rtn;
    }


    /*
     * 加密
     */
    public static byte[] encDes(String srcStr, SecretKey key) throws Exception {
        IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
        byte[] srcByte = srcStr.getBytes("UTF-8");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
        byte[] enc = cipher.doFinal(srcByte);
        return enc;
    }

    /*
     * 解密
     */
    public static byte[] decDes(byte[] srcByte, SecretKey key) throws Exception {
        IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
        byte[] dec = cipher.doFinal(srcByte);
        return dec;
    }

    public static void main(String[] args) {
        String key = "zaq1@WSX";
        //String data = "{accessToken: a07d13c1b7b44207848bef5a74a98c31,areaId: 999998,resourceId: 219,unicode: 61f7e38e91f94279b3101690bf237c8f,usId: 204881,uuid: b903cdd520b6f207de4cafb5dad2a492}";
        //String data = "{\"accessToken\":\"1a22fb0be85f4b19a736723025a98c17\",\"areaId\":\"999998\",\"resourceId\":\"123\",\"usId\":206838,\"unicode\":\"d8a17a0ec3f04f5f97fa1f5fdc236f9a\"}";
        //String data = "{\"address\":\"智汇大厦\",\"createTime\":\"2015-06-30 11:21:27\",\"headPhoto\":\"158131%2F2a4680c2b38041ae8c5c7eacd52cc3fd.jpeg\",\"nickname\":\"小王\",\"phoneNo\":15067173438,\"phoneNoStr\":\"15067173438\",\"sex\":1,\"usId\":158131,\"userName\":\"wyl20032005\",\"workUnit\":\"恒生电子\"}";

//    	String data = "{\"access_token\":\"1a22fb0be85f4b19a736723025a98c17\",\"unicode\":\"d8a17a0ec3f04f5f97fa1f5fdc236f9a\",\"client_id\":\"999998@123\",\"timestamp\":1458615290040}";
        String data = "{\"access_token\":\"1a22fb0be85f4b19a736723025a98c17\",\"timestamp\":1458615290040}";

        String newData = encrypt(data, key);
        System.out.println(newData);
        String oldData = decrypt(newData, key);
        System.out.println(oldData);
    }
}
